﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Numerics;
using System.Text;
using System.Threading.Tasks;

namespace UtilLib
{
    /// <summary>
    /// 扩展
    /// </summary>
    public static partial class Util
    {
        /// <summary>
        /// 比较两个List相等 
        /// 数量相等，元素值相等即为True；与元素顺序无关；
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="ListA"></param>
        /// <param name="ListB"></param>
        /// <returns></returns>
        public static bool ListSameElement<T>(this List<T> ListA, List<T> ListB)
        {
            return (ListA.Count == ListB.Count && ListA.Count(it => !ListB.Contains(it)) == 0);
        }

        /// <summary>
        /// 添加不重复
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="source"></param>
        /// <param name="item"></param>
        public static void TryAdd<T>(this ICollection<T> source, T item)
        {
            if (!source.Contains(item))
            {
                source.Add(item);
            }
        }

        /// <summary>
        /// 移除成员
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="source"></param>
        /// <param name="collection"></param>
        public static void RemoveAll<T>(this List<T> source, IEnumerable<T> collection)
        {
            foreach (var item in collection)
            {
                source.Remove(item);
            }
        }
        /// <summary>
        /// 移除成员
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="source"></param>
        /// <param name="predicate"></param>
        public static void RemoveAll<T>(this List<T> source, Func<T, bool> predicate)
        {
            var sourceselect = source.Where(predicate);
            foreach (var item in sourceselect)
            {
                source.Remove(item);
            }
        }
        /// <summary>
        /// 分组
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="source"></param>
        /// <param name="groupCount">分组数目</param>
        /// <returns></returns>
        public static List<List<T>> SubGroup<T>(this List<T> source, int groupCount)
        {
            var listlistresult = new List<List<T>>();
            int 平均数目 = source.Count / groupCount;
            for (int i = 0; i < groupCount; i++)
            {
                if (i == groupCount - 1)
                {
                    var listresult = source.GetRange(i * 平均数目, source.Count - (平均数目 * (groupCount - 1)));
                    listlistresult.Add(listresult);
                }
                else
                {
                    var listresult = source.GetRange(i * 平均数目, 平均数目);
                    listlistresult.Add(listresult);
                }
            }
            return listlistresult;
        }
        /// <summary>
        /// 确保最大数目
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="source"></param>
        /// <param name="count"></param>
        /// <param name="listremove"></param>
        public static void EnsureMaxCount<T>(this List<T> source, int count, out List<T> listremove)
        {
            listremove = new List<T>();
            while (source.Count > count)
            {
                listremove.Add(source[0]);
                source.RemoveAt(0);
            }
        }
    }
}
